import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Queue;

public class Sort {
    private static void swap(int[] array,int i,int j){
        int tmp = array[i];
        array[i] = array[j];
        array[j] = tmp;
    }
    public static void insertSort(int[] array){
        for (int i = 1; i < array.length; i++) {
            int tmp = array[i];
            int index = i - 1;
            while(index >= 0){
                if(array[index] < tmp){
                    break;
                }else{
                    array[index + 1] = array[index];
                }
                index--;
            }
            array[index + 1] = tmp;
        }
    }

    public static void shellSort(int[] array){
        int gap = array.length/2;
        while(gap >= 1){
            shell(array,gap);
            gap/= 2;
        }
    }

    private static void shell(int[] array, int gap) {
        for (int i = gap; i < array.length; i++) {
            int tmp = array[i];
            int index = i - gap;
            while(index >= 0){
                if(tmp > array[index]){
                    break;
                }else{
                    array[index + gap] = array[index];
                }
                index -= gap;
            }
            array[index + gap] = tmp;
        }
    }

    // 选择排序
    public static void selectSort(int[] array){
        int left = 0;
        int right = array.length-1;
        while(left <= right){
            int minIndex = findMin(left,right,array);
            int maxIndex = findMax(left,right,array);
            swap(array,minIndex,left);
            if(left == maxIndex){
                maxIndex = minIndex;
            }
            swap(array,maxIndex,right);
            left++;
            right--;
        }
    }

    private static int findMin(int left,int right,int[] array){
        int ret = left;
        for (int i = left; i <= right; i++) {
            if(array[i] < array[ret]){
                ret = i;
            }
        }
        return ret;
    }

    private static int findMax(int left,int right,int[] array){
        int ret = right;
        for (int i = left; i <= right; i++) {
            if(array[i] > array[ret]){
                ret = i;
            }
        }
        return ret;

    }


// 堆排序
    public static void heapSort(int[] array){
        createHeap(array);
        for (int i = array.length - 1; i>0; i--) {
            swap(array,i,0);
            siftDown(array,0,i);
        }
    }

    private static void createHeap(int[] array){
        int parent = (array.length-1-1)/2;
        while(parent >= 0){
            siftDown(array,parent,array.length);
            parent--;
        }
    }

    private static void siftDown(int[] array,int parent,int length){
        int child = parent*2 + 1;
        while(child < length ){
            if((child + 1) < length && array[child] <array[child + 1]){
                child++;
            }
            if(array[parent] < array[child]){
                swap(array,parent,child);
                parent = child;
                child = parent*2 + 1;
            }else{
                break;
            }
        }
    }

    // 冒泡排序
    public static void bubbleSort(int[] array){
        for (int i = 0; i < array.length; i++) {
            boolean flg = true;
            for (int j = 0; j < array.length-i-1; j++) {
                if(array[j] > array[j + 1]){
                    swap(array,j,j+1);
                    flg = false;
                }
            }
            if(flg){
                break;
            }
        }
    }

    // 快速
    public static void quickSort(int[] array){
        // write code  here
        quickSortChild(array,0,array.length-1);
    }

    private static void quickSortChild(int[] array,int left,int right){

        if(left >= right){
            return;
        }
        if(right - left <= 7){
            insertSort(array,left,right);
        }
        int midIndex = findMid(array,left,right);
        swap(array,midIndex,left);
        int pivot = partition(array,left,right);


        quickSortChild(array,left,pivot-1);
        quickSortChild(array,pivot+1,right);

    }

    private static void insertSort(int[] array, int left, int right) {
        for (int i = left + 1; i <= right; i++) {
            int tmp = array[i];
            int index = i - 1;
            while(index >= left){
                if(array[index] < tmp){
                    break;
                }else{
                    array[index + 1] = array[index];
                }
                index--;
            }
            array[index + 1] = tmp;
        }
    }

    private static int partition2(int[] array, int left, int right) {
        int cur = left + 1;
        int prev = left;
        while(cur <= right){
            if(array[cur] < array[left] && array[++prev] != cur){
                swap(array,cur,prev);
            }
            cur++;
        }
        swap(array,left,prev);
        return prev;
    }


    private static int partition(int[] array, int left, int right) {
        int tmp = array[left];

        while(left < right){
            while(left < right && array[right] >= tmp){
                right--;
            }
            if(left<right){
                swap(array,right,left);
            }
            while(left < right && array[left] <= tmp){
                left++;
            }

            swap(array,left,right);
        }
        array[left] = tmp;
        return left;
    }

    private static int findMid(int[] array, int left, int right) {
        int mid = (left+right)/2;
        if(array[left] < array[right]){
            if(array[mid] < array[left]){
                return left;
            } else if (array[mid] > array[right]) {
                return right;
            }else{
                return mid;
            }
        }else{
            if(array[mid] > array[left]){
                return left;
            } else if (array[mid] < array[right]) {
                return right;
            }else{
                return mid;
            }
        }
    }


    private static int partitionHoare(int[] array,int left,int right){
        int tmp = left;
        while(left < right){
            while(left < right && array[right] >= array[tmp]){
                right--;
            }
            while(left < right && array[left] <= array[tmp]){
                left++;
            }
            swap(array,left,right);
        }
        swap(array,tmp,left);
        return left;
    }

    public static void quickSortNor(int[] array){
        int start = 0;
        int end = array.length-1;
        Deque<Integer> stack = new ArrayDeque<>();
        int pivot = partition(array,start,end);
        if(pivot > start + 1){
            stack.push(start);
            stack.push(pivot - 1);
        }
        if(pivot <end - 1){
            stack.push(pivot + 1);
            stack.push(end);
        }
        while(!stack.isEmpty()){
            end = stack.pop();
            start = stack.pop();
            pivot = partition(array,start,end);
            if(pivot > start + 1){
                stack.push(start);
                stack.push(pivot - 1);
            }
            if(pivot <end - 1){
                stack.push(pivot + 1);
                stack.push(end);
            }
        }
    }

    public static void mergeSort(int[] array){
        mergeSortTmp(array,0,array.length-1);
    }

    private static void mergeSortTmp(int[] array, int start, int end) {
        if(start >= end){
            return;
        }
        int mid = (start+end)/2;
        mergeSortTmp(array,start,mid);
        mergeSortTmp(array,mid+1,end);
        merge(array,start,mid,end);
    }

    private static void merge(int[] array, int start, int mid, int end) {
        int[] tmp = new int[end - start + 1];
        int index = 0;
        int s1 = start;
        int s2 = mid + 1;
        int e1 = mid;
        int e2 = end;
        while(s1 <= e1 && s2 <= e2){
            if(array[s1] < array[s2]){
                tmp[index++] = array[s1++];
            }else{
                tmp[index++] = array[s2++];
            }
        }
        while(s1 <= e1){
            tmp[index++] = array[s1++];
        }
        while(s2 <= e2){
            tmp[index++] = array[s2++];
        }
        for (int i = 0; i < tmp.length; i++) {
            array[start+i] = tmp[i];
        }



    }

    public static void mergeSortNor(int[] array) {
        int gap = 1;
        while (gap < array.length) {
            for (int i = 0; i < array.length; i = i + gap * 2) {
                int left = i;
                int mid = left + gap - 1;
                if(mid >= array.length) {
                    mid = array.length-1;
                }
                int right = mid + gap;
                if(right >= array.length) {
                    right = array.length-1;
                }
                merge(array,left,mid,right);
            }
            gap *= 2;
        }
    }












}
